<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>锐一后台Robot统计</title>
    <div th:include="layout/resources :: resources"></div>
</head>

<body>
<div>
    <div class="container-fluid">
        <div id="toolbar"><label>是否在线</label><select onchange="refresh();"
                                                     style="height: 34px;
    font-size: 14px;
    line-height: 1.42857;
    color: rgb(85, 85, 85);
    background-color: rgb(255, 255, 255);
    background-image: none;
    box-shadow: rgba(0, 0, 0, 0.075) 0px 1px 1px inset;
    padding: 6px 12px;
    border-width: 1px;
    border-style: solid;
    border-color: rgb(204, 204, 204);
    border-image: initial;
    border-radius: 4px;
    transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;" id="online">
            <option value="true">是</option>
            <option value="false">否</option>
        </select>
            <button class="btn btn-default" onclick="changeConnectionIp();">切换连接</button>
            <button class="btn btn-default" onclick="changeModel();">修改设备型号</button>
            <button class="btn btn-default" onclick="releaseAll();">释放所有IP</button>
            <label>vpn当前在线数:<span id="vpnOnline"></span></label>
        </div>
        <table style="width: 2500px;" class="table-responsive" id="robotList"></table>
    </div>
</div>
</body>
<script type="text/javascript">
    $('#robotList').on('load-success.bs.table', function () {
        $(".bs-checkbox").css({'vertical-align': 'middle'});
    });
    $(function () {
        clock();
        var int = self.setInterval(clock, 5000)
    })

    $("#robotList").framework().bootstrapTable({
        url: "/robot/getListByPage",
        uniqueId: 'simCode',
        height: 800,
        pageSize: 100,
        pageList: [100, 200, 500, 'ALL'],
        queryParams: function (params) {
            params.online = $("#online").val();
            return params;
        },
        columns: [{
            checkbox: true
        }, {
            field: '',
            title: '序号',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return index + 1;
            }
        }, {
            field: 'operate',
            title: '操作',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<a href="javascript:choseModel(\'' + row.simCode + '\');">修改设备信息</a>';
            }
        }, {
            field: 'groupName',
            title: '组名',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (!value) {
                    return '<a href="javascript:updateGroupName(\'' + row.simCode + '\',\'' + value + '\');">设置组名</a>';
                } else {
                    return '<a href="javascript:updateGroupName(\'' + row.simCode + '\',\'' + value + '\');">' + value + '</a>';
                }
            }
        }, {
            field: 'nickName',
            title: '别名',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (!value) {
                    return '<a href="javascript:updateNickName(\'' + row.simCode + '\');">设置别名</a>';
                } else {
                    return '<a href="javascript:updateNickName(\'' + row.simCode + '\');">' + value + '</a>';
                }
            }
        }, {
            field: 'brand',
            title: '品牌',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (!value) {
                    return '<a href="javascript:updateByParamType(\'' + row.simCode + '\',' + 0 + ');">设置品牌</a>';
                } else {
                    return '<a href="javascript:updateByParamType(\'' + row.simCode + '\',' + 0 + ');">' + value + '</a>';
                }
            }
        }, {
            field: 'model',
            title: '型号',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (!value) {
                    return '<a href="javascript:updateByParamType(\'' + row.simCode + '\',' + 1 + ');">设置型号</a>';
                } else {
                    return '<a href="javascript:updateByParamType(\'' + row.simCode + '\',' + 1 + ');">' + value + '</a>';
                }
            }
        }, {
            field: 'wifi.ssid',
            title: 'Wifi',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (!value) {
                    return '<a href="javascript:updateWifi(\'' + row.simCode + '\');">修改Wifi</a>';
                } else {
                    return '<a href="javascript:updateWifi(\'' + row.simCode + '\');">' + value + '</a>';
                }
            }
        }, {
            field: 'simCode',
            title: 'SimCode',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'createDate',
            title: 'Obd启用时间',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'online',
            title: '是否在线',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (false == value) {
                    return "否"
                } else {
                    return "是"
                }
            }
        }, {
            field: 'busy',
            title: '是否忙碌',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (false == value) {
                    return "否"
                } else {
                    return "是"
                }
            }
        }, {
            field: 'taskRemark',
            title: '当前任务',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'offLineTime',
            title: '离线时间',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (row.online == false) {
                    return value;
                }
            }
        }, {
            field: 'version',
            title: '版本号',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'osVersion',
            title: '系统版本',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'apps',
            title: 'Apps',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'onlineIp',
            title: '上线IP',
            align: 'center',
            valign: 'middle'
        }]
    });
    function clock() {
        $.ajax({
            method: "get",
            url: createUrl("/taskDetail/getVpnOnline"),
            success: function (result) {
                $("#vpnOnline").text(result);
            }
        })
    }

    function showDetail(simCode) {
        framework.openWindow("/admin/robot/toTaskList?simCode=" + simCode + "", "80%", "60%", "任务详情");
    }
    function refresh() {
        $('#robotList').bootstrapTable('refresh');
    }
    function updateGroupName(simCode, oldGroupName) {
        layer.prompt({title: '输入組名，并确认', formType: 2}, function (newGroupName, index) {
            $.ajax({
                method: "get",
                url: createUrl('/robot/updateGroupName?simCode=' + simCode + "&newGroupName=" + newGroupName + "&oldGroupName=" + oldGroupName),
                success: function (result) {
                    if (result.success) {
                        refresh();
                        framework.alert("修改成功");
                    } else {
                        framework.alert("修改失敗:" + result.error);
                    }
                    layer.close(index);
                }
            })
        });
    }

    function updateNickName(simCode) {
        layer.prompt({title: '输入别名，并确认', formType: 2}, function (nickName, index) {
            $.ajax({
                method: "get",
                url: createUrl('/robot/updateNickName?simCode=' + simCode + "&nickName=" + nickName),
                success: function (result) {
                    if (result.success) {
                        refresh();
                        framework.alert("修改成功");
                    } else {
                        framework.alert("修改失敗:" + result.error);
                    }
                    layer.close(index);
                }
            })
        });
    }
    function updateByParamType(simCode, type) {
        layer.prompt({title: '输入内容，并确认', formType: 2}, function (param, index) {
            $.ajax({
                method: "get",
                url: createUrl('/robot/updateByParamType?simCode=' + simCode + "&param=" + param + "&type=" + type),
                success: function (result) {
                    if (result.success) {
                        refresh();
                        framework.alert("修改成功");
                    } else {
                        framework.alert("修改失败:" + result.error);
                    }
                    layer.close(index);
                }
            })
        });
    }
    function changeModel() {
        $.ajax({
            method: "get",
            url: createUrl('/robot/changeModel'),
            success: function (result) {
                if (result.success) {
                    refresh();
                    framework.alert("修改成功");
                } else {
                    framework.alert("修改失敗:" + result.error);
                }
                layer.close(index);
            }
        })
    }
    function updateWifi(simCode) {
        framework.openWindow('/robot/toUpdateWifi?simCode=' + simCode, "80%", "70%", "修改Wifi")
    }
    function choseModel(simCode) {
        framework.openWindow("/robot/toUpdateModel?simCode=" + simCode, "80%", "70%", "选择设备型号");
    }

    function releaseAll() {
        $.ajax({
            method: "get",
            url: createUrl('/robot/releaseAll'),
            success: function (result) {
                if (result.success) {
                    framework.alert("释放成功");
                } else {
                    framework.alert("释放失败:" + result.error);
                }
                layer.close(index);
            }
        })
    }

    function changeConnectionIp() {
        var selected = $('#robotList').bootstrapTable('getSelections');
        var simCodes = [];
        for (row in selected) {
            simCodes.push(selected[row].simCode);
        }
        layer.prompt({title: '输入IP，并确认', formType: 2}, function (ip, index) {
            layer.close(index);
            $.ajax({
                method: "post",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    ip: ip,
                    simCodes: simCodes
                }),
                url: createUrl('/robot/changeConnectionIp'),
                success: function (result) {
                    if (result.success) {
                        framework.alert("释放成功");
                    } else {
                        framework.alert("释放失败:" + result.error);
                    }
                    layer.close(index);
                }
            })
        });

    }
</script>
</html>
